package com.lake.banner.uitls;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Environment;
import android.os.Build.VERSION;
import android.text.TextUtils;
import android.util.Base64;
//import com.alipay.iot.sdk.utils.ErrorCode.DetectionErrorCode;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;

public class SystemUtils {
    public static final String TAG = SystemUtils.class.getSimpleName();

    public SystemUtils() {
    }

    public static String getAlipayRoot(Context context) {
        return context.getFilesDir().getAbsolutePath();
    }

    public static String getRuntimeRoot(Context context) {
        return context.getFilesDir().getAbsolutePath();
    }

    public static String getLoggerRoot() {
        String basePath = Environment.getExternalStoragePublicDirectory("").getAbsolutePath() + "/alipay/iotsdk";
//        if (FileUtils.mkDir(basePath)) {
////            AlipayIoTLogger.e(TAG, "Create SDK_STORAGE_ROOT dir failed", new Object[0]);
//        }

        return basePath;
    }

    public static String getSerialNumber() {
        String serial = "";

        try {
            serial = Build.SERIAL;
            if ((TextUtils.isEmpty(serial) || "unknown".equalsIgnoreCase(serial)) && VERSION.SDK_INT >= 26) {
                serial = Build.getSerial();
            }
        } catch (Error var2) {
        } catch (Exception var3) {
        }

        if (TextUtils.isEmpty(serial) || "unknown".equalsIgnoreCase(serial)) {
            serial = getSystemProperty("ro.serialno", (String)null);
            if (TextUtils.isEmpty(serial)) {
                serial = "unknown";
            }
        }

        return serial;
    }

    public static boolean isSupportMasterInstall() {
        String masterConfig = getMasterInstallConfig();
        return masterConfig != null && masterConfig.equals("install");
    }

    private static String getMasterInstallConfig() {
        String env = null;

        try {
            String filePath = getLoggerRoot() + "/.master_config";
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            char[] buf = new char[7];
            if (reader.read(buf, 0, 7) > 0) {
                env = String.copyValueOf(buf);
            }

            reader.close();
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return env;
    }

    public static int getDetectionLevel() {
        int level = -1;
        String filePath = getLoggerRoot() + "/.iot_config";
        String configStr = readFile(filePath);
        if (configStr != null && !configStr.isEmpty()) {
            try {
                JSONObject config_obj = new JSONObject(configStr);
                level = config_obj.optInt("detection_level");
            } catch (JSONException var4) {
            }
        }

        return level;
    }

//    public static int setDetectionLevel(int level) {
//        int res = 0;
//
//        try {
//            String filePath = getLoggerRoot() + "/.iot_config";
//            String currentConfigStr = readFile(filePath);
//            JSONObject configObj;
//            if (currentConfigStr != null && !currentConfigStr.isEmpty()) {
//                configObj = new JSONObject(currentConfigStr);
//                int currentLevel = configObj.getInt("detection_level");
//                if (level >= currentLevel) {
//                    return res;
//                }
//
//                configObj.put("detection_level", level);
//            } else {
//                configObj = new JSONObject();
//                configObj.put("detection_level", level);
//            }
//
//            writeToFile(filePath, configObj.toString());
//        } catch (IOException var6) {
//            res = DetectionErrorCode.LOCAL_SDK_CONFIG_FILE_IO_ERROR.value();
////            AlipayIoTLogger.e(TAG, "setDetectionLevel error : " + res, new Object[0]);
//        } catch (JSONException var7) {
//            res = DetectionErrorCode.LOCAL_SDK_CONFIG_FILE_IO_ERROR.value();
////            AlipayIoTLogger.e(TAG, "setDetectionLevel error : " + res, new Object[0]);
//        }
//
//        return res;
//    }

    public static String getAlipayProfile() {
        String profile = getSystemProperty("ro.product.alipay.profile", (String)null);
        if (profile != null && !profile.isEmpty()) {
            byte[] data = Base64.decode(profile, 11);
            if (data != null) {
                return new String(data, Charset.forName("UTF-8"));
            }
        }

        return "";
    }

    public static boolean isPackageInstalled(Context context, String pkgName) {
        if (context != null || !TextUtils.isEmpty(pkgName)) {
            List<PackageInfo> packageInfoList = context.getPackageManager().getInstalledPackages(0);
            if (packageInfoList != null && packageInfoList.size() > 0) {
                Iterator var3 = packageInfoList.iterator();

                while(var3.hasNext()) {
                    PackageInfo packageInfo = (PackageInfo)var3.next();
                    if (packageInfo.packageName.equals(pkgName)) {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    public static String getVersionFromPackage(Context context, String packageName) {
        String version = "NULL";
        if (packageName != null && !packageName.isEmpty()) {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = null;

            try {
                packageInfo = packageManager.getPackageInfo(packageName, 0);
                if (packageInfo != null) {
                    version = packageInfo.versionName;
                    version = version + "." + packageInfo.versionCode;
                }
            } catch (NameNotFoundException var6) {
//                AlipayIoTLogger.e(TAG, " Get target package version name error: " + var6.getMessage(), new Object[0]);
            }
        }

        return version;
    }

    public static String getSystemProperty(String key, String defaultValue) {
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class, String.class);
            return (String)((String)get.invoke(c, key, defaultValue));
        } catch (Exception var4) {
//            AlipayIoTLogger.e(TAG, "get system property failed: " + var4.getMessage(), new Object[]{var4});
            return defaultValue;
        }
    }

    public static int compareVersionNames(String oldVersionName, String newVersionName) {
        int res = 0;
        String[] oldNumbers = oldVersionName.split("\\.");
        String[] newNumbers = newVersionName.split("\\.");
        int maxIndex = Math.min(oldNumbers.length, newNumbers.length);

        for(int i = 0; i < maxIndex; ++i) {
            int oldVersionPart = Integer.valueOf(oldNumbers[i]);
            int newVersionPart = Integer.valueOf(newNumbers[i]);
            if (oldVersionPart < newVersionPart) {
                res = -1;
                break;
            }

            if (oldVersionPart > newVersionPart) {
                res = 1;
                break;
            }
        }

        if (res == 0 && oldNumbers.length != newNumbers.length) {
            res = oldNumbers.length > newNumbers.length ? 1 : -1;
        }

        return res;
    }

    public static void writeToFile(String filePath, String result) throws IOException {
        File file = new File(filePath);
        if (!file.isFile()) {
            file.createNewFile();
        }

        DataOutputStream out = new DataOutputStream(new FileOutputStream(file));
        out.writeBytes(result);
        out.close();
    }

    public static String readFile(String path) {
        File file = new File(path);
        BufferedReader reader = null;
        String laststr = "";

        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;

            for(int line = 1; (tempString = reader.readLine()) != null; ++line) {
                System.out.println("line " + line + ": " + tempString);
                laststr = laststr + tempString;
            }

            reader.close();
        } catch (Exception var14) {
//            AlipayIoTLogger.e(TAG, "Exception: " + var14.getMessage(), new Object[0]);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException var13) {
                }
            }

        }

        return laststr;
    }

    public static String getNetworkEnvConfig() {
        String env = null;

        try {
            String filePath = getLoggerRoot() + "/.sdk_env";
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            char[] buf = new char[3];
            if (reader.read(buf, 0, 3) > 0) {
                env = String.copyValueOf(buf);
            }

            reader.close();
        } catch (IOException var4) {
        }

        return env;
    }
}
